<template>
  <div class="app-container">
    <searchBox @handleSearch="handleQuery" @handleReset="resetQuery">
      <el-form :model="queryParams.param" ref="CustomerForm" :inline="true" label-width="auto" label-position="left">
        <el-form-item label="UID" prop="id">
          <el-input v-model.trim="queryParams.param.id" placeholder="请输入UID" clearable />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model.trim="queryParams.param.username" placeholder="请输入用户名" clearable />
        </el-form-item>
        <el-form-item label="注册手机" prop="phone">
          <el-input v-model.trim="queryParams.param.phone" placeholder="请输入手机号" clearable />
        </el-form-item>
        <el-form-item label="注册邮箱" prop="email">
          <el-input v-model.trim="queryParams.param.email" placeholder="请输入名称" clearable />
        </el-form-item>
        <el-form-item label="所属站点" prop="websiteId">
          <el-select v-model="queryParams.param.websiteId" placeholder="请选择" clearable filterable reserve-keyword
            style="width: 190px">
            <el-option v-for="dict in host_type" :key="dict.id" :label="dict.name" :value="dict.id"
              :disabled="!dict.status"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="注册时间" prop="time">
          <el-date-picker v-model="queryParams.param.newTime" type="datetimerange" start-placeholder="开始日期"
            end-placeholder="结束日期" format="YYYY-MM-DD HH:mm:ss" date-format="YYYY/MM/DD ddd" time-format="A hh:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss">
          </el-date-picker>
        </el-form-item>
      </el-form>
    </searchBox>
    <el-table :data="draftList" style="height: calc(100vh - 360px)" v-loading="loading">
      <el-table-column label="UID" prop="id" width="100" />
      <el-table-column label="用户名" prop="username" />
      <el-table-column label="昵称" prop="nickName" />
      <el-table-column label="注册手机" prop="phone" />
      <el-table-column label="注册邮箱" prop="email" />
      <el-table-column label="注册时间" align="center" prop="newTime" />
      <el-table-column label="所属站点" prop="websiteName" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="handleDetail(scope.row)">详细信息</el-button>
          <el-button link type="primary" @click="handleTradeInformation(scope.row)">交易记录</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 详细信息 -->
    <DetailDialog :listId="listId" @handleDialogClose="handleDialogClose" :detailOpen="detailOpen" />
    <!-- 交易记录 -->
    <TradeInformationDialog :listId="listId" @handleDialogClose="handleDialogClose"
      :tradeInformationOpen="tradeInformationOpen" />
  </div>
</template>

<script setup name="UserManage">
import { reactive, ref, onMounted } from "vue";
import DetailDialog from "./components/DetailDialog.vue";
import TradeInformationDialog from "./components/TradeInformationDialog.vue";
const { proxy } = getCurrentInstance();
const host_type = ref([]); //来源网站
const listId = ref(null);
const detailOpen = ref(false); // 是否显示弹出层
const loading = ref(true)

const tradeInformationOpen = ref(false);
const total = ref(0);
const draftList = ref([]);
const openCollect = ref(false);
const collectLoading = ref(false);
// 列表请求参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 20,
  param: {},
});
const data = reactive({
  form: {},
  rules: {
    title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
    price: [{ required: true, message: "价格不能为空", trigger: "blur" }],
  },
});
const { form, rules } = toRefs(data);

onMounted(() => {
  getList();
  getLanguage();
});
const handleDialogClose = () => {
  detailOpen.value = false;
  tradeInformationOpen.value = false;
};
// 详细信息按钮
const handleDetail = (e) => {
  listId.value = e.id;
  detailOpen.value = true;
};
// 交易信息按钮
const handleTradeInformation = (e) => {
  listId.value = e.id;
  tradeInformationOpen.value = true;
};
/** 查询列表 */
const getList = () => {
  product.productcustomer
    .productcustomerList({
      data: queryParams.value,
    })
    .then((res) => {
      loading.value = false
      total.value = res.total;
      if (res.data?.length) {
        res.data = res.data.map((code) => {
          return {
            ...code,
            newTime: code.created ? proxy.DateTime(code.created) : "",
          };
        });
      }
      draftList.value = res.data;
      // console.log(res.data, "[[[[[]]]]]");
    });
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.param.startTime=null
  queryParams.value.param.endTime=null
  if (queryParams.value.param.newTime.length) {
    queryParams.value.param.startTime = proxy.convertTime(1, queryParams.value.param.newTime[0]);
    queryParams.value.param.endTime = proxy.convertTime(1, queryParams.value.param.newTime[1]);
  }
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    param: {},
  };
  getList();
};

const getLanguage = () => {
  // 所属站点
  site.general
    .sitewebsiteList({
      data: {
        pageNum: 1,
        pageSize: 999,
      },
    })
    .then((res) => {
      host_type.value = res.data;
    });
};
</script>
